package financial.sharing.center.system.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.utils.StringUtil;
import financial.sharing.center.system.dao.BusinessLineMapper;
import financial.sharing.center.system.model.entity.BusinessLine;
import financial.sharing.center.system.service.BusinessLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BusinessLineServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/6
 * @Version 1.0
 */
@Service
public class BusinessLineServiceImpl implements BusinessLineService {

    @Autowired
    private BusinessLineMapper businessLineMapper;

    private BusinessLine getBusinessLine(Integer id){
        return this.businessLineMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<BusinessLine> getAllBusinessLine(String selectBusinessLineName, String selectSubCompany, String selectDepartment) {
        Example example = new Example(BusinessLine.class);
        example.createCriteria().andLike("businessLineName", StringUtil.toLike(selectBusinessLineName))
                                .andLike("subCompany",StringUtil.toLike(selectSubCompany))
                                .andLike("department",StringUtil.toLike(selectDepartment))
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        List<BusinessLine> businessLineList = this.businessLineMapper.selectByExample(example);
        return new PageInfo<>(businessLineList);
    }

    private BusinessLine getBusinessLine(String businessLineName){
        Example example = new Example(BusinessLine.class);
        example.createCriteria().andEqualTo("businessLineName",businessLineName)
                                .andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        return this.businessLineMapper.selectOneByExample(example);
    }

    @Override
    public Integer createOneBusinessLine(String businessLineName, String subCompany, String department, String note) {
        if (this.getBusinessLine(businessLineName) != null){
            return ResponseContext.已经存在.getValue();
        }
        BusinessLine businessLine = new BusinessLine(businessLineName,subCompany,department,note);
        int i = this.businessLineMapper.insert(businessLine);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneBusinessLine(BusinessLine businessLine) {
        int i = this.businessLineMapper.updateByPrimaryKey(businessLine);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneBusinessLine(Integer id) {
        BusinessLine businessLine = this.getBusinessLine(id);
        businessLine.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.businessLineMapper.updateByPrimaryKey(businessLine);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public List<BusinessLine> getListBusinessLine() {
        Example example = new Example(BusinessLine.class);
        example.createCriteria().andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        List<BusinessLine> businessLineList = this.businessLineMapper.selectByExample(example);
        return businessLineList;
    }

    @Override
    public Map<Integer, String> getMapBusinessLine() {
        List<BusinessLine> businessLineList = this.getListBusinessLine();
        Map<Integer,String> businessLineMap = new HashMap<>();
        for (BusinessLine businessLine : businessLineList) {
            businessLineMap.put(businessLine.getId(), businessLine.getBusinessLineName());
        }
        return businessLineMap;
    }
}
